﻿using System;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using SelfMadeMillionaire.Core.Services;
using SelfMadeMillioniare.API.Helpers;
using SelfMadeMillioniare.API.TransportObjects;
using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.BLL.Services;
using SelfMadeMillionaire.DAL.Repositories;
using SelfMadeMillionaire.DAL;
using SelfMadeMillionaire.Core.Enums;

namespace SelfMadeMillioniare.API.Controllers
{
    public class BaseApiController : ApiController
    {
        public BaseApiController()
        {
            Response = ResponseHelper.GetResponse();
            CurrentRequest = HttpContext.Current.Request;
        }

        protected HttpRequest CurrentRequest { get; set; }
        protected Response Response { get; set; }

        
        protected const string AuthTokenField = "auth_token";
        protected const string UserNameField = "username";
        protected const string PasswordField = "password";
        protected const string EmailField = "email";

        
        
        private Subscriber currentUser = null;
        protected Subscriber CurrentUser
        {
            get { return currentUser; }
        }
       
        protected bool IsUserAuth(string token)
        {
            using (var ctx = new DataContext())
            {
                var _subscriberService = new SubscriberService(new SubscriberRepository(ctx));

                currentUser = _subscriberService.GetByAuthToken(token);
                return currentUser != null;
            }
        }

        protected SubscriptionType GetMinAccessLevel(string auth_token)
        {
            var minAccessLevel = SubscriptionType.Free;

            using (var ctx = new DataContext())
            {
                var service = new SubscriberService(new SubscriberRepository(ctx));
                var user = service.GetByAuthToken(auth_token);
                if (user != null)
                    minAccessLevel = service.GetMinAccessLevel(user.Email);
            }
            
            return minAccessLevel;
        }
    }
}
